<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/4/22
 * Time: 9:49
 */

namespace app\extra\lib;


use app\index\model\Url;

class ImgCut
{
    protected $url_img;
    protected $img_name;

    public function __construct($url,$name)
    {
        $this->url_img=$url;
        $this->img_name=$name;
    }

    public function CutTo($newheight,$newwidth,$LocalRoot)
    {
        $arrimg=getimagesize($this->url_img);
        $thumb=imagecreatetruecolor($newwidth,$newheight);
        $source=null;
        switch ($arrimg[2])
        {
            case 2:
                $source=imagecreatefromjpeg($this->url_img);
                break;
            case 3:
                $source=imagecreatefrompng($this->url_img);
                break;
            default:
                $source=imagecreatefromjpeg($this->url_img);
                break;
        }
        imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$arrimg[0],$arrimg[1]);
        switch ($arrimg[2])
        {
            case 2:
                imagejpeg($thumb,$LocalRoot.$this->img_name);
                break;
            case 3:
                imagepng($thumb,$LocalRoot.$this->img_name);
                break;
            default:
                imagejpeg($thumb,$LocalRoot.$this->img_name);
                break;
        }
    }

    public function CutALL()
    {
        $size[0][0]=128;$size[0][1]=128;
        $size[1][0]=200;$size[1][1]=400;
        $size[2][0]=256;$size[2][1]=256;
        $size[3][0]=300;$size[3][1]=450;
        $size[4][0]=400;$size[4][1]=200;
        $localRoot[0]=Url::ImgLocal_128_128();
        $localRoot[1]=Url::ImgLocal_200_400();
        $localRoot[2]=Url::ImgLocal_256_256();
        $localRoot[3]=Url::ImgLocal_300_450();
        $localRoot[4]=Url::ImgLocal_400_200();
        for($i=0;$i<5;$i++)
        {
            $newwidth=$size[$i][0];
            $newheight=$size[$i][1];
            $arrimg=getimagesize($this->url_img);
            $thumb=imagecreatetruecolor($newwidth,$newheight);
            $source=null;
            switch ($arrimg[2])
            {
                case 2:
                    $source=imagecreatefromjpeg($this->url_img);
                    break;
                case 3:
                    $source=imagecreatefrompng($this->url_img);
                    break;
                default:
                    $source=imagecreatefromjpeg($this->url_img);
                    break;
            }
            imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$arrimg[0],$arrimg[1]);
            switch ($arrimg[2])
            {
                case 2:
                    imagejpeg($thumb,$localRoot[$i].$this->img_name);
                    break;
                case 3:
                    imagepng($thumb,$localRoot[$i].$this->img_name);
                    break;
                default:
                    imagejpeg($thumb,$localRoot[$i].$this->img_name);
                    break;
            }
        }
    }
}